'use strict';

require('../model/merchantInfo.model');

const mongoose = require('mongoose'),
    merchantInfoModel = mongoose.model('merchantInfo');

module.exports = {
    getById: getById,
    getBystatus: getBystatus,
    updateStatus2: updateStatus2,
    gets: gets,
    updateStatus1: updateStatus1,
    updateWithReajectRea: updateWithReajectRea,
    insertOne: insertOne,
    getByName: getByName,
    update: update,
    getByIcum: getByIcum
}

function getById(id, callback){
    merchantInfoModel.findOne({"_id":id},function(error, data){
        callback(null, data);
    });
}

function getBystatus(status,callback){
    merchantInfoModel.find({status1 : status},function(error, data){
        callback(null,data);
    });
}

function updateStatus2(id, status2, callback){
    merchantInfoModel.update({"_id" : id},{
        $set: {"status2":status2 ,"operationTime":new Date()}
    },function(error, data){
        console.log(data)
        callback(error,data);
    });
}

function updateStatus1(id, status1, callback){
    merchantInfoModel.findByIdAndUpdate({"_id" : id},{
        $set: {"status1":status1}
    },function(error, data){
        console.log(data)
        callback(error,data);
    });
}

function updateWithReajectRea(id, status1, rejectrea, callback){
    merchantInfoModel.update({"_id":id},{
        $set: {"status1":status1,'rejectRea':rejectrea}
    },function(error, data){
        callback(null, data);
    });
}

function gets(callback){
    merchantInfoModel.find({},function(error, data){
        callback(null, data);
    });
}

function insertOne(merchant, callback){
    merchantInfoModel.create(merchant,function(error, data){
        callback(null, data);
    });
}

function getByName(name, callback){
    merchantInfoModel.findOne({"name":name},function(error, data){
        callback(null, data);
    });
}

function update(merchant, callback){
    console.log(merchant)
    merchantInfoModel.update({"name":merchant.name},merchant,function(error, data){
        callback(null, data);
    });
}

function getByIcum(icnum, callback){
    merchantInfoModel.findOne({"icNum":icnum},function(error, data){
        callback(null, data);
    });
}

